// pages/content/content.js
var netWork = require('../../assets/js/network.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initSrc:'',
    content:{},
    dataList:[],
    generalize:{
      src:'',
      href:''
    },
    commit:true,
    comment_area:[],
    commits:''
  },
  init:function(obj){
    var This = this;
    wx.request({
      url: `${netWork.api}wzshortfilm/info/${obj}`,
      method: 'GET',
      success: function (res) {
        res.data.wzShortfilm.shortfilmTime = res.data.wzShortfilm.shortfilmTime.replace(':','：')
        This.setData({
          content: res.data.wzShortfilm
        },()=>{This.initList()})
        This.getListCom(res.data.wzShortfilm.pid)
      }
    })
  },
  initList:function(){
    var This = this;
    wx.request({
      url: `${netWork.api}wzshortfilm/list?pageSize=6&currentPage=1&sidx=''&order=asc&typePid=''&shortfilmName=''&lablePid=${This.data.content.lablePid}`,
      method: 'GET',
      success: function (res) {
        This.setData({
          dataList: res.data.page.list
        })
      }
    })
  }, 
  getListCom: function (e) {
    var This = this;
    wx.request({
      url: `${netWork.api}wzcomments/list?pageSize=3&currentPage=1&sidx=sysCreateTime&order=desc&shortfilmPid=${e}`,
      method: 'GET',
      success: function (res) {
        This.setData({
          comment_area: res.data.page.list
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    this.init(option.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.createVideoContext('playVideo').play();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (e) {
    var init = getCurrentPages();
    if (init.length > 3){
       wx.reLaunch({
         url: `../../${init[0].route}`
       })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('到底了')
  },
  comments:function(){
    this.setData({
      commit:true
    })
  },
  inputDown:function(e){
    this.setData({
      commits: e.detail.value
    })
  },
  pushCommit:function(){
    
    var This = this;
    wx.request({
      url: `${netWork.api}wzcomments/save`,
      method: 'POST',
      data:{
        "shortfilmPid": This.data.content.pid,
        "userPid": wx.getStorageSync('token'),
        "commentsContent": This.data.commits
      },
      success: function (res) {
        if(!res.code){
          This.getListCom(This.data.content.pid)
          wx.showToast({
            title: '评论成功',
            icon: 'success',
            mask:true,
            duration: 2000
          })
        }
      }
    })
  },
  allCommit:function(e){
    wx.navigateTo({
      url: `../../pages/discuss/discuss?id=${e.currentTarget.dataset.id}`
    })
  },
  img1: function (e) {
    wx.navigateTo({
      url: `../../pages/content/content?id=${e.currentTarget.dataset.id}`
    })
  }
})